//
//  ViewController.m
//  UISearchBar
//
//  Created by lirenqiang on 2016/11/8.
//  Copyright © 2016年 1111. All rights reserved.
//

#import "ViewController.h"
#import "ILCSearchController.h"



@interface ViewController () <UISearchBarDelegate>
@property (nonatomic, strong)UISearchBar *ilcSearchBar;
@property (nonatomic, strong) UISearchController *searchC;
@property (nonatomic, strong) UITableViewController *tbViewC;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    NSString *a1 = @"  a1";
    NSString *a2 = @"a2  ";
    NSString *a3 = @" a 3";
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:a1, a2, a3, nil];
    for (int i = 0; i < arr.count; i++) {
        arr[i] = [self deleteAllSpace:arr[i]];
    }
    
    for (int i = 0; i < 3; i++) {
        NSLog(@"%@", arr[i]);
    }
    
    NSString *str = @" 1 2    3 ";
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    UIView *searchView = [UIView new];
    searchView.backgroundColor = [UIColor colorWithRed:201.0/255.0 green:201.0/255.0 blue:206.0/255.0 alpha:1.0];
    searchView.frame = CGRectMake(0, 200, kScreenW, 44);
//    [self.view addSubview:searchView];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(8, 8, kScreenW-16, 28)];
    btn.adjustsImageWhenHighlighted = NO;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
    btn.backgroundColor = [UIColor whiteColor];
    [btn setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [btn setTitle:@"联系方式/订单号(后4位)" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 0)];
    [btn addTarget:self action:@selector(searchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [searchView addSubview:btn];
    
    UIView *tfView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, kScreenW, 44)];
    UIImageView *tfImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 44)];
    tfImage.image = [UIImage imageNamed:@"searchbar_bg_small_radius"];
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, kScreenW, 44)];
    tf.placeholder = @"123455";
    tf.text = @"";
    
    [tfView addSubview:tfImage];
    [tfView addSubview:tf];
    [self.view addSubview:tfView];
    
    
    
    self.ilcSearchBar.frame = CGRectMake(0, 100, kScreenW, 44);
    //搜索框的placeholder
    self.ilcSearchBar.placeholder = @"hello";
//    self.ilcSearchBar.prompt = @"world";
    // 光标的颜色
    self.ilcSearchBar.tintColor = [UIColor redColor];
    //设置搜索框的背景颜色
//    self.ilcSearchBar.barTintColor = [UIColor whiteColor];
    //搜索框的样式
//    self.ilcSearchBar.barStyle = UISearchBarStyleDefault;
    //搜索框的透明度
    self.ilcSearchBar.translucent = YES;
//    [self.view addSubview:self.ilcSearchBar];
    
//    self.ilcSearchBar.showsCancelButton = YES;
//    self.ilcSearchBar.showsBookmarkButton = YES;
//    self.ilcSearchBar.showsSearchResultsButton = YES;
    
    self.ilcSearchBar.delegate = self;
//    self.ilcSearchBar.scopeButtonTitles = @[@"123",@"456"];
//    self.ilcSearchBar.showsScopeBar = YES;
    
    
//    [self.view addSubview:self.ilcSearchBar];
    
//    self.navigationController.navigationItem.titleView = self.ilcSearchBar;
    
}
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self.ilcSearchBar setShowsCancelButton:YES animated:NO];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"搜索" forState:UIControlStateNormal];
            cancel.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    self.ilcSearchBar.showsCancelButton = NO;
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    self.ilcSearchBar.showsCancelButton = NO;
    [self.view endEditing:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.view endEditing:YES];
}

- (UISearchBar *)ilcSearchBar {
    if (!_ilcSearchBar) {
        _ilcSearchBar = [[UISearchBar alloc] init];
    }
    return _ilcSearchBar;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableViewController *)tbViewC {
    if (!_tbViewC) {
        _tbViewC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    }
    return _tbViewC;
}

- (UISearchController *)searchC {
    if (!_searchC) {
        _searchC = [[UISearchController alloc] initWithSearchResultsController:self.tbViewC];
    }
    return _searchC;
}


- (void)searchBtnClick
{
    
    UITableViewController *tbVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
    UISearchController *searchVC = [[UISearchController alloc] initWithSearchResultsController:tbVC];
    searchVC.view.backgroundColor = [UIColor whiteColor];
    tbVC.tableView.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:searchVC animated:YES];
}
- (IBAction)nextController:(id)sender {
    ILCSearchController *ilcC = [ILCSearchController new];
    [self.navigationController pushViewController:ilcC animated:NO];
}


- (NSString *)deleteAllSpace:(NSString *)str
{
    return [str stringByReplacingOccurrencesOfString:@" " withString:@""];
}

@end
